package com.dhcc.core.framework.security.client.form;

import java.security.MessageDigest;

/**
 * 常見的几种加密方式
 */
public class Encrypt {
    public static void main(String[] args) {
        //Scanner sc = new Scanner(System.in);
        //String next = sc.next(); //123
        //String md5str = string2MD5(next);                //202CB962AC59075B964B07152D234B70
        //String sha1str = string2Sha1(next);            //40bd001563085fc35165329ea1ff5c5ecbdbbeef
        //String sha256str = String2SHA256(next); 			//a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3
        //String sha256Javastr = String2SHA256StrJava(next);    //a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3
    }

    /**
     * MD5加密 生成32位md5码
     */
    public static String string2MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = (byte) charArray[i];
        }
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString().toUpperCase();
    }

    /**
     * SHA1加密
     *
     * @param str
     * @return
     */
    public static String string2Sha1(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));

            byte[] md = mdTemp.digest();
            int j = md.length;
            char buf[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     *  利用Apache的工具类实现SHA-256加密
     *  所需jar包下載 http://pan.baidu.com/s/1nuKxYGh
     * @param str 加密后的报文
     * @return
     */
   /* public static String String2SHA256(String str){
        MessageDigest messageDigest;
        String encdeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] hash = messageDigest.digest(str.getBytes("UTF-8"));
            encdeStr = Hex.encodeHexString(hash);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encdeStr;
    }*/
    //---------
    /**
     * 利用java原生的摘要实现SHA256加密
     * @param str 加密后的报文
     * @return
     */
   /* public static String String2SHA256StrJava(String str){
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            encodeStr = byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encodeStr;
    }*/

    /**
     * 将byte转为16进制
     *
     * @param bytes
     * @return
     */
    @SuppressWarnings("unused")
	private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                //1得到一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

    /**
     * 描述:cache加密</br>
     * Author:baimingzhe</br>
     * 日期:2018年8月13日</br>
     * 参数:@param bytes
     * 参数:@return</br>
     * 返回类型:String
     */
    @SuppressWarnings("unlikely-arg-type")
	public static String dhc_cacheEncrypt(String pass) {
        int constval = 37;
        String out = "";
        //pass = pass.replace(/(\s*$)/g,"");
        pass = pass.replaceAll("\\s+$", ""); // 去掉右侧末尾的空格;
        int len;
        int num;
        char ch;
        for (int i = 1; i <= pass.length(); i++) {
            ch = pass.charAt(i - 1);
            if (ch == 'D') {
                ch = (char) 2;
            }
            if (ch == 'p') {
                ch = (char) 3;
            }
            if (ch == 'd') {
                ch = (char) 4;
            }
            if (ch == 't') {
                ch = (char) 5;
            }

            num = ch;  //char转int
            num = (num - i + constval) % 255;
            if (num > 127) {
                num = (num + 128) % 255;
            }
            if (num < 32) {
                num = (num + 40) % 255;
            }
            if ("^".equals((char) num)) {
                num++;
            }
            out += "" + (char) (num % 255);

        }
        len = out.length();
        for (int i = len; i < 12; i++) {
            ch = out.charAt(i - len);
            num = ch;
            num = (int) (num * 2.345 * constval * (constval - 7)) % 255;
            if (num > 127) {
                num = (num + 128) % 255;
            }
            if (num < 32) {
                num = (num + 40) % 255;
            }
            if ("^".equals((char) num)) {
                num++;
            }
            out += "" + (char) (num % 255);
        }
        return out;
    }

    public static String decrypt_Password(String pin) {
        String out = "";
        boolean flag = false;
        int length = pin.length();
        for (int i = 0; i < length; i++) {
            if (flag) {
                break;
            }
            for (int j = 42; j < 127; j++) {
                String tempOut = out + (char) j;
                String enc = dhc_cacheEncrypt(tempOut);
                if (enc.equals(pin)) {
                    out = tempOut;
                    flag = true;
                    break;
                }
                ///$e???????????????
                if (enc.charAt(i) == pin.charAt(i)) {
                    out = tempOut;
                    break;
                }
            }
        }
        return out;
    }


}
